using System;
using System.Collections;

namespace LazyLoadDotNet.Domain
{
    public class LoaderRegistry
    {
        private static Hashtable registry;

        static LoaderRegistry()
        {
            if (registry != null) return;
            registry = new Hashtable();
            registry.Add(typeof (Address), new AddressLoader());
        }
        
        public static void RegisterLoader<T>(ILoader<T> loader) where T : IGhostable
        {
            if (registry.ContainsKey(typeof(T)))
                registry[typeof (T)] = loader;
            else 
                registry.Add(typeof(T), loader);
        }

        public static ILoader<T> GetLoader<T>() where T : IGhostable
        {
            if (registry.ContainsKey(typeof(T)))
                return (ILoader<T>) registry[typeof (T)];
            throw new ApplicationException(string.Format("Unable to find loader for type {0}", typeof (T).FullName));
        }
    }
}